//
//  KSLMenuToolBar.m
//  KSLMenuToolBar
//
//  Created by kensla on 14-8-12.
//  Copyright (c) 2014年 kensla. All rights reserved.
//

#import "KSLMenuToolBar.h"
//#import "KSLMenuToolBarItem.h"
#import "KSLMenuToolBarButton.h"
#import "KSLMenu.h"
#define kXHMenuButtonBaseTag 100
@interface KSLMenuToolBar(){
    
}

@property (nonatomic, strong) NSMutableArray *menuToolBarItems;

@end


@implementation KSLMenuToolBar

#pragma mark - Propertys

- (NSMutableArray *)menuToolBarItems {
    if (!_menuToolBarItems) {
        _menuToolBarItems = [[NSMutableArray alloc] initWithCapacity:self.menus.count];
    }
    return _menuToolBarItems;
}

#pragma mark - Action

- (void)setSelectedIndex:(NSUInteger)selectedIndex animated:(BOOL)aniamted calledDelegate:(BOOL)calledDelgate {
    
    _selectedIndex = selectedIndex;
    if ([(id)self.delegate respondsToSelector:@selector(itemSelectedAtIndex:)])
    {
        [self.delegate itemSelectedAtIndex:_selectedIndex];
    }
    
}


- (KSLMenuToolBarButton *)getButtonWithMenu:(KSLMenu *)menu {
    
    KSLMenuToolBarButton *menuToolBarItem;
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        
        menuToolBarItem =[[KSLMenuToolBarButton alloc] initWithFrame:CGRectMake(0, 0, (CGRectGetWidth(self.bounds))*1/4, CGRectGetHeight(self.bounds))];
        
    }else{
        
        menuToolBarItem =[[KSLMenuToolBarButton alloc] initWithFrame:CGRectMake(10, 0, 80, CGRectGetHeight(self.bounds))];
    }

    menuToolBarItem.backgroundColor =menu.backgroundColor;
    menuToolBarItem.layer.cornerRadius = 5;
    
    
    //menuToolBarItem.imageView.image =[UIImage imageNamed:menu.iconName];
    
    menuToolBarItem.titleLabel.font = menu.titleFont;
    
    [menuToolBarItem setImage:[UIImage imageNamed:menu.iconName] forState:UIControlStateNormal];
    
    [menuToolBarItem setTitle:menu.title forState:UIControlStateNormal];
    
    [menuToolBarItem addTarget:self action:@selector(menuItemSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return menuToolBarItem;
}




- (void)menuItemSelected:(KSLMenuToolBarButton *)sender {

    [self setSelectedIndex:sender.tag - kXHMenuButtonBaseTag animated:YES calledDelegate:YES];
}


- (void)reloadData {
    [menuToolBarView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[KSLMenuToolBarButton class]]) {
            [((KSLMenuToolBarButton *)obj) removeFromSuperview];
        }
    }];
    if (self.menuToolBarItems.count)
        [self.menuToolBarItems removeAllObjects];
    
    for (KSLMenu *menu in self.menus) {
        
        
        NSUInteger index = [self.menus indexOfObject:menu];
        
        KSLMenuToolBarButton *menuToolBarItem =[self getButtonWithMenu:menu];
        
        menuToolBarItem.tag = kXHMenuButtonBaseTag + index;
        
        CGRect menuViewFrame = menuToolBarItem.frame;
        
        CGFloat buttonX = 0;
        
        if (index) {
            buttonX = KSLMenuButtonPaddingX + CGRectGetMaxX(((KSLMenuToolBarButton *)(self.menuToolBarItems[index - 1])).frame);
        } else {
            buttonX = KSLMenuButtonStarX;
        }
        
        menuViewFrame.origin = CGPointMake(buttonX, CGRectGetMidY(self.bounds) - (CGRectGetHeight(menuViewFrame) / 2.0));
        
        menuToolBarItem.frame = menuViewFrame;
        
        [menuToolBarView addSubview:menuToolBarItem];
        
        [self.menuToolBarItems addObject:menuToolBarItem];
        
        if (self.selectedIndex == index) {
            menuToolBarItem.selected = YES;
        }
       
    }
    
    //[self setSelectedIndex:self.selectedIndex animated:NO calledDelegate:YES];
    
}





- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    menuToolBarView = [[UIView alloc] initWithFrame:self.bounds];
    
    [self addSubview:menuToolBarView];
}

@end
